package cn.com.aiton.educenter.controller;

import cn.com.aiton.educenter.config.OperLog;
import cn.com.aiton.educenter.entity.PestsQrcode;
import cn.com.aiton.educenter.entity.vo.PestsQrcodeQuery;
import cn.com.aiton.educenter.service.PestsQrcodeService;
import cn.com.qiter.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 实体，二维码 前端控制器
 * </p>
 *
 * @author testAuthor
 * @since 2021-02-24
 */
@RestController
@RequestMapping("/educenter/qrcode")
public class PestsQrcodeController {
    //把service注入
    @Autowired
    private PestsQrcodeService pestsQrcodeService;

    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有二维码列表")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "所有二维码列表")
    @GetMapping("findAll")
    public Result findAll() {
        //调用service的方法实现查询所有的操作
        List<PestsQrcode> list = pestsQrcodeService.list(null);
        return Result.ok().data("items",list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除")
    @OperLog(type = "删除",module = "PestsProjectController",requestDes = "逻辑删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "ID", required = true)
                         @PathVariable String id) {
        boolean flag = pestsQrcodeService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分布查询")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "分布查询")
    @GetMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current,
                       @PathVariable long limit) {
        //创建page对象
        Page<PestsQrcode> pageTeacher = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        pestsQrcodeService.page(pageTeacher,null);

        long total = pageTeacher.getTotal();//总记录数
        List<PestsQrcode> records = pageTeacher.getRecords(); //数据list集合

        return Result.ok().data("total",total).data("rows",records);
    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "分布查询")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "分布查询")
    @PostMapping("page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,@PathVariable long limit,
                                @RequestBody(required = false) PestsQrcodeQuery teacherQuery) {
        //创建page对象
        Page<PestsQrcode> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<PestsQrcode> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String deviceId = teacherQuery.getCodeNumber();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(deviceId)) {
            //构建条件
            wrapper.like("code_number",deviceId);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("stime");

        //调用方法实现条件查询分页
        pestsQrcodeService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<PestsQrcode> records = pageTeacher.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师接口的方法
    @ApiOperation(value = "新增二维码")
    @OperLog(type = "新增",module = "PestsProjectController",requestDes = "新增二维码")
    @PostMapping("save")
    public Result save(@RequestBody PestsQrcode eduTeacher) {
        boolean save = pestsQrcodeService.save(eduTeacher);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //根据讲师id进行查询
    @ApiOperation(value = "根据二维码查询一条记录")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "根据二维码查询一条记录")
    @GetMapping("getOneByQrcode/{codeNumber}")
    public Result getOneByQrcode(@PathVariable String codeNumber) {
        QueryWrapper<PestsQrcode> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(codeNumber)) {
            //构建条件
            wrapper.eq("code_number",codeNumber);
        }
        PestsQrcode eduTeacher = pestsQrcodeService.getOne(wrapper);
        return Result.ok().data("teacher",eduTeacher);
    }
    //根据讲师id进行查询
    @ApiOperation(value = "根据ID查询一条记录")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "根据ID查询一条记录")
    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable String id) {
        PestsQrcode eduTeacher = pestsQrcodeService.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }

    //讲师修改功能
    @ApiOperation(value = "更新")
    @OperLog(type = "更新",module = "PestsProjectController",requestDes = "更新")
    @PostMapping("update")
    public Result update(@RequestBody PestsQrcode eduTeacher) {
        boolean flag = pestsQrcodeService.updateById(eduTeacher);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    @ApiOperation(value = "生成二维码")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "生成二维码")
    @GetMapping("initOne/{id}/{codeInit}/{type}")
    public Result initOne(@PathVariable String id,@PathVariable int codeInit,@PathVariable String type) throws Exception {

        boolean flag = pestsQrcodeService.initOne(id,codeInit,type);
        if (flag)
            return Result.ok();
        else
            return Result.error();
    }
    @ApiOperation(value = "批量生成二维码")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "批量生成二维码")
    @GetMapping("batchtrap/{id}/{start}/{limit}/{type}")
    public Result batchTrapQrcode(@PathVariable String id,@PathVariable int start,@PathVariable int limit,@PathVariable String type) throws Exception {

        boolean flag = pestsQrcodeService.batchTrap(id,start,limit,type);
        if (flag)
            return Result.ok();
        else
            return Result.error();
    }
    @ApiOperation(value = "批量生成二维码")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "批量生成二维码")
    @GetMapping("batch/{id}/{start}/{limit}/{type}")
    public Result batchQrcode(@PathVariable String id,@PathVariable int start,@PathVariable int limit,@PathVariable String type) throws Exception {

        boolean flag = pestsQrcodeService.batch(id,start,limit,type);
        if (flag)
            return Result.ok();
        else
            return Result.error();
    }


    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "统计二维码")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "统计二维码")
    @GetMapping("countQrcodeAll")
    public Result countQrcodeAll() {
        Integer count = pestsQrcodeService.getBaseMapper().selectCount(null);
        return Result.ok().data("count",count);
    }
}

